package com.mayisoftware.mayioa.business.plan.impl.controller;

import com.mayisoftware.mayioa.business.plan.api.domain.PlanAchievement;
import com.mayisoftware.mayioa.business.plan.api.domain.PlanClass;
import com.mayisoftware.mayioa.business.plan.api.domain.PlanConstraint;
import com.mayisoftware.mayioa.business.plan.api.domain.PlanCustom;
import com.mayisoftware.mayioa.business.plan.api.service.IPlanConstraintService;
import com.mayisoftware.mayioa.common.api.annotation.Log;
import com.mayisoftware.mayioa.common.api.base.AjaxResult;
import com.mayisoftware.mayioa.common.api.enums.BusinessType;
import com.mayisoftware.mayioa.common.api.enums.RuleTarget;
import com.mayisoftware.mayioa.common.impl.utils.StringUtils;
import com.mayisoftware.mayioa.platform.api.domain.SysDictData;
import com.mayisoftware.mayioa.platform.api.service.ISysDictDataService;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import com.mayisoftware.mayioa.platform.impl.web.page.TableDataInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 约束配置
 */
@Controller
@RequestMapping("/plan/constraint")
public class PlanConstraintController extends BaseController
{
    private String prefix = "plan/constraint";

    @Autowired
    private IPlanConstraintService planConstraintService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查看约束配置
     */
    @RequiresPermissions("plan:constraint:view")
    @GetMapping()
    public String planConstraint()
    {
        return prefix + "/constraint";
    }

    /**
     * 新增约束
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {

        return prefix + "/add";
    }

    /**
     * 新增保存约束配置
     */
    @RequiresPermissions("plan:constraint:add")
    @Log(title = "约束配置管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult addSave(PlanConstraint planConstraint)
    {
        planConstraint.setPlanCstrUpdateUserId(ShiroUtils.getUserId());
        return toAjax(planConstraintService.insertPlanConstraint(planConstraint));
    }

    /**
     * 修改约束配置
     */
    @GetMapping("/edit/{planCstrId}")
    public String edit(@PathVariable("planCstrId") Long planCstrId, ModelMap mmap)
    {
        PlanConstraint cstr = planConstraintService.selectPlanConstraintById(planCstrId);
        mmap.put("planConstraint",cstr);

        mmap.put("itemOption",this.selectCstrValuesByType(cstr.getPlanCstrObj()));

        return prefix + "/edit";
    }

    /**
     * 修改保存约束配置
     */
    @RequiresPermissions("plan:constraint:edit")
    @Log(title = "约束配置管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(PlanConstraint planConstraint)
    {
        planConstraint.setPlanCstrUpdateUserId(ShiroUtils.getUserId());
        return toAjax(planConstraintService.updatePlanConstraint(planConstraint));
    }

    @RequiresPermissions("plan:constraint:remove")
    @Log(title = "约束配置管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(planConstraintService.deletePlanConstraintByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 校验约束配置名称唯一
     */
    @PostMapping("/checkPlanCstrNameUnique")
    @ResponseBody
    public String checkPlanCstrNameUnique(PlanConstraint planConstraint)
    {
        return planConstraintService.checkPlanCstrNameUnique(planConstraint);
    }

    /**
     * 根据规则对象类型查询可选的键值
     * @param param
     * @return
     */
    @GetMapping("/selectCstrValuesByType")
    @ResponseBody
    public List<Map<String, Object>> selectCstrValuesByType(String param)
    {
        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        if(StringUtils.isEmpty(param))
        {
            return dataList;
        }

        String dictType = RuleTarget.getType(Integer.parseInt(param));

        Map<String, Object> dataMap = null;

        List<SysDictData> cstrList = dictDataService.selectDictDataByType(dictType);
        if(CollectionUtils.isNotEmpty(cstrList))
        {
            for (SysDictData dictData : cstrList)
            {
                dataMap = new HashMap<>();
                dataMap.put("itemId", dictData.getDictValue());
                dataMap.put("itemName", dictData.getDictLabel());
                dataList.add(dataMap);
            }
        }

        return dataList;
    }

    /**
     * 查询约束配置列表
     */
    @RequiresPermissions("plan:constraint:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PlanConstraint planConstraint)
    {
        startPage();
        List<PlanConstraint> list = planConstraintService.selectPlanConstraintList(planConstraint);
        return getDataTable(list);
    }

}
